package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/modules/resume/model"
)

type ResumeTemplateMapper struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *ResumeTemplateMapper) GetById(id uint64, systemId uint64) (resumeTemplate *model.ResumeTemplate, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	dbResumeTemplateTemplate := model.ResumeTemplate{Id: id, SystemId: systemId}
	// 3: 读取id的数据放入到结构体，进行返回
	err = db.Read(&dbResumeTemplateTemplate, "id", "system_id")
	// 4: 结构返回
	return &dbResumeTemplateTemplate, err
}

/*
*保存笔记
 */
func (mapper *ResumeTemplateMapper) SaveUpdateResumeTemplate(resumeTemplate model.ResumeTemplate) uint64 {
	newOrm := orm.NewOrm()
	if resumeTemplate.Id > 0 {
		_, _ = newOrm.Update(&resumeTemplate)
		return resumeTemplate.Id
	} else {
		_, _ = newOrm.Insert(&resumeTemplate)
		return resumeTemplate.Id
	}
}

/**
 * @desc 更新文件状态
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *ResumeTemplateMapper) UpdateResumeTemplateStatus(id uint64, status int, systemId uint64) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("update xk_resume_template set status = ?,update_time = now()  where id = ? and system_id = ?", status, id, systemId).Exec()
	if err != nil {
		return false
	}
	return true
}

/**
 * @desc 更新文件状态
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *ResumeTemplateMapper) DelResumeTemplate(id uint64, systemId uint64) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("delete from  xk_resume_template  where id = ? and system_id = ?", id, systemId).Exec()
	if err != nil {
		return false
	}
	return true
}

/**
 * @desc 更新下载次数
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *ResumeTemplateMapper) UpdateResumeTemplateDownNum(id uint64, systemId uint64) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("update xk_resume_template set download_num = download_num + 1 ,update_time = now()  where id = ? and system_id = ?", id, systemId).Exec()
	if err != nil {
		return false
	}
	return true
}

/**
 * @desc 根据类型查询广告
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 * SELECT * FROM xk_resume_template WHERE `status` = 1 AND is_deleted = 0  AND type = 2 AND start_time <=NOW() AND end_time >=NOW() ORDER BY sorted ASC;
 */
func (service *ResumeTemplateMapper) FindResumeTemplate(keyword string, rtype int, systemId uint64) []*model.ResumeTemplate {
	// 1：准备容器对象，开始装载数据库数据
	resumeList := []*model.ResumeTemplate{}
	// 2: 创建一个数据连接
	mysql := orm.NewOrm()
	// 3: 指定查询的表
	qs := mysql.QueryTable("xk_resume_template")
	// 4: 创建查询条件
	whereCondition := orm.NewCondition()
	whereCondition = whereCondition.
		And("is_deleted", 0).
		And("system_id", systemId).
		And("status", 1)
	if rtype > 0 {
		whereCondition = whereCondition.And("rtype", rtype)
	}
	if len(keyword) > 0 {
		whereCondition = whereCondition.And("title__icontains", keyword)
	}
	// 4: 执行查询
	_, err := qs.SetCond(whereCondition).OrderBy("sorted").All(&resumeList)
	if nil != err {
		return nil
	}
	return resumeList
}
